<?php

class Controller_Pages extends Controller_Base
{
	private $show_in_page = 7;
	
	public function __construct($parts) {		
		$this->parts        = $parts;
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."pages";
		$this->tree_site    = Registry::get("tree_site");
	}
	
	public function once_items($sql = false)
	{
		$r = & Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` WHERE ".$sql." `Show` = '1' AND `Parent`='".$this->common_vars["structure_id"]."' LIMIT 1;");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if ($r->numRows())
		{
			$item = $r->fetchRow(MDB2_FETCHMODE_ASSOC);			
			/* Add Array for Title */
			Registry::set("title", functions::createTitle(array($this->common_vars["structure_name"], $item["name"]), $item["title"]));
			/* SEO */
			Registry::set("description", $item["description"]);
			Registry::set("keywords",    $item["keywords"]);
			/* Если есть формы в тексте */			
			$btext_form = functions::findFormInText($item["btext"]);
			if($btext_form) {
				$item["btext"] = $btext_form;
			}			
			/* END Если есть формы в тексте */				
			return array(
				"name"      => $item["name"],
				"btext"     => $item["btext"],
				"is_header" => $item["is_header"],
				"steidzh_image" => functions::getImage("top_steidzh", $item["id"], $item["name"], 1, "382x187_top_trumbnail.jpg", "normal", "md5"),
				"steidzh_text" => $item["text_steidzh"]
			);			
		}		
	}

    public function out()
	{     
		$this->all_count = functions::count_position($this->table_module, $this->common_vars["structure_id"]);				
		
		/* Если одна запись */
        if ($this->all_count == 1 && empty($this->parts[2])) {		
			$out = self::once_items();
        }
		/* Если больше одной записи */
		elseif($this->all_count > 1) 
		{
		    /* Список */
			if(empty($this->parts[2]) || ($this->parts[2] == 'page')) 
			{ 
				$_PAGING = new navygation_page(Registry::get("MDB"), $this->parts[1], $this->parts[3]);
				$r = & $_PAGING->get_page("SELECT * FROM `".$this->table_module."` WHERE `Show`='1' AND `Parent`='".$this->common_vars["structure_id"]."' ORDER BY `Por` ");
                if (PEAR::isError($r)) {
                    die($r->getMessage().' ::: '.$r->getUserInfo());
                }				
				if($r->numRows()) {					
					while($item = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) 	{
						$item["image"] = functions::getImage("pages", $item["id"], $item["name"], 1, "_106x75_pages_trumbnail.jpg", "normal", "md5");
						$out['c_array'][] = $item;				
					}					
					$out['navygation']['prev'] = $_PAGING->get_prev_page_link();
					$out['navygation']['next'] = $_PAGING->get_next_page_link();
					$out['navygation']['pages'] = $_PAGING->get_page_links();
					$out['navygation']['result_text'] = $_PAGING->get_result_text();														
				}				
				Registry::set("title", functions::createTitle(array($this->common_vars["structure_name"]), ''));
            }
			else {
                $out = self::once_items("`Sys_name` = '".$this->parts[2]."' AND ");
            }			
        }
		
		$out['header'] = $this->tree_site[0]["name"];
		$out['header_section'] = $this->common_vars["structure_name"];
		$out['section'] = $this->parts[1];
		
		//$if_menu = (!in_array($this->parts[1], Registry::get("NoneLeftMenuSection")) ? menu::getMainMenu(true, $this->tree_site[0]["id"]) : '');
		$out['menu'] = (!in_array($this->parts[1], Registry::get("NoneLeftMenuSection")) ? menu::getMainMenu(true, $this->tree_site[0]["id"]) : '');
		
		/* Меню новостей справа */
		$out['menu_news'] = menu::getSubMenuNews(56);		
		/* Меню акции справа */
		// $out['menu_action'] = menu::getSubMenuNews(57);	
		
		
		// print_r($out);
		// print_r($this->tree_site);
		Registry::get("smarty")->assign($out);
		return Registry::get("smarty")->fetch(TEMPLATE_SITE.$this->common_vars["controller_template"], Registry::get("cache_id"));	
    }
}

$controller = new Controller_Pages($parts);
$site_module_out = $controller->out();

?>